package viewcontroller;

import java.util.ArrayList;
import model.Store;
import com.example.lastprojectbeautiplus.R;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.viewpagerindicator.CirclePageIndicator;


import utils.Event;
import utils.GridViewItem;
import utils.ServiceHandler;
import adapter.HomeGridViewAdapter;
import adapter.HomeViewPagerAdapter;
import android.app.Fragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;

public class StoreViewFragment extends Fragment {
	public StoreViewFragment() {
	}

	Store store;
	private ArrayList<Event> eventList;
	private Event[] event;
	ArrayList<GridViewItem> bannerList;
	HomeViewPagerAdapter pagerAdapter;
	ViewPager vpStore;
	CirclePageIndicator vpIndicator;
	GridView gvCategory;
	HomeGridViewAdapter gridviewAdapter;
	ImageView imgBanner, imgLogo;
	int height, width;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_store_view,
				container, false);
		Bundle extras = getActivity().getIntent().getBundleExtra(
				"FromStoreListView");
		final int target = extras.getInt("StoreID");
		DisplayMetrics metrics = rootView.getResources().getDisplayMetrics();
		width = metrics.widthPixels;
		height = metrics.heightPixels;
		eventList = new ArrayList<Event>();
		bannerList = new ArrayList<GridViewItem>();

		vpStore = (ViewPager) rootView.findViewById(R.id.vpStoreEvent);
		vpStore.getLayoutParams().height = height * 3 / 10;
		vpStore.getLayoutParams().width = width;

		imgBanner = (ImageView) rootView.findViewById(R.id.imgStoreBanner);
		imgLogo = (ImageView) rootView.findViewById(R.id.imgStoreLogo);
		imgBanner.getLayoutParams().width = width;
		imgBanner.getLayoutParams().height = height / 3;
		imgLogo.getLayoutParams().width = width / 5;
		imgLogo.getLayoutParams().height = width / 5;

		// Event
		vpIndicator = (CirclePageIndicator) rootView
				.findViewById(R.id.vpStoreIndicator);
		// Call Store info
		String urlStoreView = "http://services-beautyplus.rhcloud.com/rest/StoreServices/Stores/"
				+ target;
		new GetStore(urlStoreView).execute();
		// MakeGridview
		gvCategory = (GridView) rootView.findViewById(R.id.gvShopCategory);
		AddItemToGridView();
		gridviewAdapter = new HomeGridViewAdapter(getActivity(),
				R.layout.gridview_item, bannerList);
		gvCategory.setAdapter(gridviewAdapter);
		gvCategory.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (bannerList.get(position).getTarget() == 1) {
					Intent intent = new Intent(getActivity(), SlideMenu.class);
					intent.putExtra("FromStoreView", target);
					getActivity().startActivity(intent);
				}
			}
		});
		return rootView;
	}

	private class getEventList extends AsyncTask<Void, Void, Void> {
		private String url;
		private String jsonEvent;

		public getEventList(String url) {
			this.url = url;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			// Creating service handler class instance
			utils.ServiceHandler sh = new utils.ServiceHandler();
			// Making a request to url and getting response
			jsonEvent = sh.executeGetRequest(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			Gson productGson = new Gson();
			event = productGson.fromJson(jsonEvent, Event[].class);
			for (int i = 0; i < event.length; i++) {
				eventList.add(event[i]);
			}
			if(event.length == 0)
				vpStore.getLayoutParams().height = 0;
			pagerAdapter = new HomeViewPagerAdapter(getActivity(), eventList);
			vpStore.setAdapter(pagerAdapter);
			vpIndicator.setViewPager(vpStore);
		}
	}

	private class GetStore extends AsyncTask<Void, Void, Void> {
		String url;
		String jsonRespone;

		public GetStore(String url) {
			this.url = url;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ServiceHandler sh = new ServiceHandler();
			jsonRespone = sh.executeGetRequest(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			Gson myGson = new Gson();
			store = myGson.fromJson(jsonRespone, Store.class);
			String bannerPath = store.getBanner_path().toString();
			String logoPath = store.getLogo_path().toString();

			Picasso.with(getActivity()).load(bannerPath)
					.placeholder(R.drawable.image_wait)
					.error(R.drawable.error_loading).into(imgBanner);

			Picasso.with(getActivity()).load(logoPath)
					.placeholder(R.drawable.image_wait)
					.error(R.drawable.error_loading).into(imgLogo);
			String urlEventStore = "http://services-beautyplus.rhcloud.com/rest/EventServices/GetEvent?storeID="
					+ store.getStoreID();
			new getEventList(urlEventStore).execute();
			super.onPostExecute(result);
		}

	}

	private void AddItemToGridView() {
		bannerList.add(new GridViewItem("Tất Cả Sản Phẩm",
				R.drawable.banner_setclothing, 1));
		bannerList.add(new GridViewItem("Sản Phẩm Mới", R.drawable.banner_new,
				2));
		bannerList.add(new GridViewItem("Sản Phẩm Bán Chạy",
				R.drawable.banner_best_sale, 3));
	}
}
